package com.sduept.nwld.dataserver.controller.daily;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.fault.protection.model.ProtectDeviceAlarm;
import com.sduept.bigdata.fault.protection.service.ProtectDeviceAlarmQueryManager;
import com.sduept.utils.DateUtil;

@Named
@ViewScoped
public class ProtectDeviceAlarmComtroller extends DailyReportAbstractController {

	private static final long serialVersionUID = 8721624424028806166L;

	@Autowired
	private ProtectDeviceAlarmQueryManager pd;
	private List<ProtectDeviceAlarm> protectDeviceAlarms = new ArrayList<>();
	private ProtectDeviceAlarm selected = null;
	private String searchLike = "";

	@PostConstruct
	public void init() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start = format.format(queryDate1);
		String end = format.format(queryDate2);
		protectDeviceAlarms = pd.getProtectDeviceAlarmByDate(start, end);
	}

	public String getLockText(Integer num) {
		String str = "是";
		if (num == null || num == 0) {
			str = "否";
		}
		return str;
	}

	public String getStyleClass(Integer num) {
		String str = "yes";
		if (num == null || num == 0) {
			str = "no";
		}
		return str;
	}

	public void refreshPage() {
		initParamter();
		init();
	}

	public void onRowSelect(SelectEvent event) {
		selected = (ProtectDeviceAlarm) event.getObject();
	}

	public void onCellEdit(CellEditEvent event) {
		selected = null;
		for (ProtectDeviceAlarm k : protectDeviceAlarms) {
			if (k.getId().equals(event.getRowKey())) {
				selected = k;
				break;
			}
		}
		if (selected != null && selected.getRevertTime() != null) {
			SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			int millSec = Integer.parseInt((dfs.format(selected.getRevertTime())).substring(20));
			selected.setmReverMs(millSec);
			long durationTime = calaulateDurationTime();
			selected.setDurationTime(durationTime);
			pd.updateProtectDeviceAlarm(selected);
		}
	}

	private long calaulateDurationTime() {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		long lastTime = 0;
		try {
			if (selected != null && selected.getRevertTime() != null) {
				int beginSS = selected.getmStartMs();
				// int endSS = selected.getmReverMs();
				String s = dfs.format(selected.getEventStart());
				String e = dfs.format(selected.getRevertTime());
				Date begin = dfs.parse(s);
				Date end = dfs.parse(e);
				lastTime = (end.getTime() - (begin.getTime() + beginSS));// 得到两者的毫秒数
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return lastTime;
	}

	public void queryByTime() throws ParseException {
		if (queryDate2 == null) {
			queryDate2 = new Date();
		}
		if (queryDate1 == null) {
			queryDate1 = new Date(0);
		}
		queryDate2 = DateUtil.addDays(queryDate2, 1);
		queryDate2 = DateUtil.addSeconds(queryDate2, -1);
		protectDeviceAlarms = pd.getProtectAlarm(queryDate1, queryDate2, searchLike);
		refreshQueryParamter();
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
	}

	public void onDateSelectStart(SelectEvent popup1) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		facesContext.addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(popup1.getObject())));
	}

	public void onDateSelectEnd(SelectEvent popup2) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		facesContext.addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(popup2.getObject())));
	}

	public List<ProtectDeviceAlarm> getProtectDeviceAlarms() {
		return protectDeviceAlarms;
	}

	public void setProtectDeviceAlarms(List<ProtectDeviceAlarm> protectDeviceAlarms) {
		this.protectDeviceAlarms = protectDeviceAlarms;
	}

	public ProtectDeviceAlarm getSelected() {
		return selected;
	}

	public void setSelected(ProtectDeviceAlarm selected) {
		this.selected = selected;
	}

	public String getSearchLike() {
		return searchLike;
	}

	public void setSearchLike(String searchLike) {
		this.searchLike = searchLike;
	}

}
